<?php

namespace app\administrative\controller;
use app\common\model\Node;
use think\Controller;
use app\common\model\Notice;
use think\Config;
use think\Cookie;
use app\common\model\User;
use think\Db;
class Base extends Controller
{

    protected $set = array();
    public function _initialize()
    {
        //初始化权限数据表
        $this->set['db_name'] =  'administrative_user';
        $this->set['auth_group'] =  'administrative_auth_group';
        $this->set['auth_group_access'] =  'administrative_auth_group_access';
        $this->set['auth_rule'] =  'administrative_auth_rule';
        $this->set['model'] =  strtolower(request()->module());
        //读取跳过验证方法
        $filter= Config::get('filter_config.'.strtolower(request()->module()));
        
        //根据表名设置session避免多个端session被覆盖
        if(!session($this->set['db_name'].'uid')||!session($this->set['db_name'].'username')){
            //判断是否存在免登cookie
            if(empty(Cookie::get(strtolower(request()->module()).'_user'))){
                $this->redirect('login/index');
            }else{
                $hasUser = Cookie::get(strtolower(request()->module()).'_user');
                
                $userOne = Db::name($this->set['db_name'])->field('status,is_delete')->where(['id'=>$hasUser['id']])->find();
                
                //判断帐号是否异常
                if($userOne['status'] == 0 || $userOne['is_delete'] == 1){
                    $this->redirect('login/index');
                }
                //获取模型
                $module     = strtolower(request()->module());
                $user = new User($this->set['db_name']);
                //获取用户分组
                $info = $user->getRoleInfo($hasUser['groupid'],$module);
                //dump($info);exit;
                session($this->set['db_name'].'uid', $hasUser['id']);         //用户ID
                session($this->set['db_name'].'username', $hasUser['account']);  //用户名
                session($this->set['db_name'].'portrait', $hasUser['avatar_img']); //用户头像
                session($this->set['db_name'].'rolename', $info['title']);    //角色名
                session($this->set['db_name'].'a_id', $hasUser['a_id']);      //行政中心ID
                session($this->set['db_name'].'is_admin', $hasUser['is_admin']);
                //如果为超管登录 清除角色节点
                if(empty($info['rules'])){
                    session($this->set['db_name'].'rule', null);        //角色节点
                }else{
                    session($this->set['db_name'].'rule', $info['rules']);        //角色节点
                }
                
                if(!empty($info['name'])){
                    session($this->set['db_name'].'name', $info['name']);         //角色权限
                }
                $param = [
                    'loginnum' => $hasUser['loginnum'] + 1,
                    'last_login_ip' => request()->ip(),
                    'last_login_time' => time()
                ];
                Db::name($this->set['db_name'])->where('id', $hasUser['id'])->update($param);
                writelog($hasUser['id'],session($this->set['db_name'].'username'),'用户【'.session($this->set['db_name'].'username').'】登录成功',1,$this->set['model'].'_log');
            }
        }
        $auth = new \com\Auth($this->set);
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = $module."/".$controller."/".$action;



        //跳过检测以及主页权限
        if(session($this->set['db_name'].'is_admin')!=1){
            if(!in_array($url, $filter)){
                if(!$auth->check($url,session($this->set['db_name'].'uid'))){
                    $this->error('抱歉，您没有操作权限');
                }
            }
        }
        $node = new Node();
        if(empty(session($this->set['db_name'].'rule')) && session($this->set['db_name'].'is_admin')!=1){
            $auth_rule = '';
        }else{
            $auth_rule = $node->getMenu(session($this->set['db_name'].'rule'),$this->set['auth_rule']);
        }
        //统计公告条数
        $map['status'] = 1;
        $map['isshow'] = 1;
        $ann_model = new Notice();
        $ann_count = $ann_model->where($map)->count();
        $this->assign(['username' => session($this->set['db_name'].'username'),
            'portrait' => session($this->set['db_name'].'portrait'),
            'rolename' => session($this->set['db_name'].'rolename'),
            'menu' => $auth_rule,//读取权限菜单
            'is_admin' => session($this->set['db_name'].'is_admin'),//是否为超管
            'ann_count' =>$ann_count
        ]);


       /*  $config = cache($this->set['db_name'].'db_config_data');

        if(!$config){
            $config = api('Config/lists');
            dump($config);exit;
            cache($this->set['db_name'].'db_config_data',$config);
        }
        config($config);  */

        /*  if(config('web_site_close') == 0 && session('uid') !=1 ){
            $this->error('站点已经关闭，请稍后访问~');
        }

        if(config('admin_allow_ip') && session('uid') !=1 ){

            if(in_array(request()->ip(),explode(',',config('admin_allow_ip')))){
                $this->error('403:禁止访问');
            }
        } */

    }
}